home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-16 | 10.5 KB | 448 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: PRAlert.cpp
- // Release Version: $ ODF 1 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWODTYPS_H
- #include "FWODTyps.h"
- #endif
-
- #ifndef PRALERT_H
- #include "PRAlert.h"
- #endif
-
- #ifndef FWRESOUR_H
- #include "FWResour.h"
- #endif
-
- #ifndef FWSTRING_H
- #include "FWString.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_Errors_defined
- #include <ErrorDef.xh>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
- #include <TextUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__ICONS__)
- #include <Icons.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__DIALOGS__)
- #include <Dialogs.h>
- #endif
-
- //========================================================================================
- // Runtime Informations
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwtoolbx
- #endif
-
- //========================================================================================
- // Macintosh Dialog Refcon structure
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- struct FW_SMacAlertRefCon
- {
- short fOkItemId;
- short fCancelItemId;
- };
- #endif
-
- #ifdef FW_BUILD_MAC
- //========================================================================================
- // MacAlertDialogFilter
- //========================================================================================
-
- static pascal Boolean MacAlertDialogFilter(DialogPtr theDialog,
- EventRecord * theEvent,
- short *itemHit)
- {
- Boolean result = FALSE;
-
- FW_SMacAlertRefCon* alertRefCon = (FW_SMacAlertRefCon*)::GetWRefCon(theDialog);
-
- switch (theEvent->what)
- {
- case keyDown:
- case autoKey:
- {
- switch (theEvent->message & charCodeMask)
- {
- case '.':
- if (theEvent->modifiers & cmdKey && alertRefCon->fCancelItemId != 0)
- {
- result = TRUE;
- *itemHit = alertRefCon->fCancelItemId;
- }
- break;
-
- case 0x0D:
- case 0x03:
- result = TRUE;
- *itemHit = alertRefCon->fOkItemId;
- break;
-
- case 0x1B:
- if (alertRefCon->fCancelItemId != 0)
- {
- result = TRUE;
- *itemHit = alertRefCon->fCancelItemId;
- }
- break;
- }
-
- if (result) // flash the button
- {
- long theTick;
- short itemType;
- Handle hItem;
- Rect box;
-
- ::GetDialogItem(theDialog, *itemHit, &itemType, &hItem, &box);
- ::HiliteControl((ControlHandle)hItem, 1);
- ::Delay(6,&theTick);
- ::HiliteControl((ControlHandle)hItem, 0);
- }
- }
- break;
-
- case updateEvt:
- if (theDialog == (DialogPtr)theEvent->message)
- {
- PenState ps;
- Rect box;
- Handle handle;
- short type;
-
- ::SetPort(theDialog);
- ::GetPenState(&ps);
- ::PenSize(3, 3);
- ::GetDialogItem(theDialog, alertRefCon->fOkItemId, &type, &handle, &box);
- ::InsetRect(&box, -4, -4);
- ::FrameRoundRect(&box, 16, 16);
- ::SetPenState(&ps);
- }
- break;
- }
-
- return result;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // MacSetButtonText
- //----------------------------------------------------------------------------------------
-
- static void MacSetButtonText(DialogPtr dialog, short buttonId, Str32 text, FW_Boolean moveButton)
- {
- Rect rect;
- short type;
- Handle handle;
-
- ::GetDialogItem(dialog, buttonId, &type, &handle, &rect);
- if (text[0] != 0)
- {
- ::SetControlTitle(ControlHandle(handle), text);
- if (moveButton)
- {
- ::OffsetRect(&rect, 10, 0);
- ::SetDialogItem(dialog, buttonId, type, handle, &rect);
- ::MoveControl(ControlHandle(handle), rect.left, rect.top);
- }
- }
- else
- {
- ::HideControl(ControlHandle(handle));
- }
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivAlert
- //----------------------------------------------------------------------------------------
- FW_AlertResult SL_API FW_PrivAlert(Environment* ev,
- FW_HString captionRep,
- FW_HString messageRep,
- FW_ButtonType buttonType,
- FW_IconType iconType,
- FW_DefaultButton defaultButton,
- FW_Boolean beep)
- {
- FW_CString caption(captionRep);
- FW_CString message(messageRep);
-
- #ifdef FW_BUILD_WIN
- UINT dialogFlags = buttonType | iconType | defaultButton | MB_TASKMODAL;
- dialogFlags ^= dialogFlags & MB_SYSTEMMODAL;
-
- // ::MessageBox() doesn't work if mouse capturing is active,
- // so we release the capture (if any) and then restore it
-
- const HWND hwndCapture = ::GetCapture();
-
- if (hwndCapture != NULL)
- ::ReleaseCapture();
-
- if (beep)
- ::MessageBeep((UINT)iconType);
-
- FW_CAcquireNulTerminatedString255 szMessage(message);
- FW_CAcquireNulTerminatedString255 szCaption(caption);
-
- short result = ::MessageBox(NULL, szMessage, szCaption, dialogFlags);
-
- // Set mouse capture to the window that had it
- if (hwndCapture != 0)
- ::SetCapture(hwndCapture);
-
- return result;
- #endif
-
- #ifdef FW_BUILD_MAC
- #define kDialogID 500
- // ----- dialog item ids
- #define kButton1Id 1
- #define kButton2Id 2
- #define kButton3Id 3
- #define kMessageId 4
- #define kIconId 5
-
- GrafPtr curPort;
- ::GetPort(&curPort);
-
- DialogPtr dlg = NULL;
- FW_VOLATILE(dlg);
-
- short items[3];
- short item;
-
- FW_TRY
- {
- // ----- Open create the dialog (can't do exception here because used to display exception messages)
- {
- FW_CAcquireCFMResourceAccess a(ev);
- dlg = ::GetNewDialog(kDialogID, NULL, WindowPtr(-1));
- }
-
- FW_SMacAlertRefCon alertRefCon;
- ::SetWRefCon(dlg, (long)&alertRefCon);
-
- Rect rect;
- short type;
- Handle handle;
-
- ::GetDialogItem(dlg, kMessageId, &type, &handle, &rect);
-
- Str255 str;
- message.ExportPascal(str);
- ::SetDialogItemText(handle, str);
-
- Str32 but1, but2, but3;
- but1[0] = 0;
- but2[0] = 0;
- but3[0] = 0;
-
- FW_Boolean moveButton = FALSE;
-
- switch (buttonType)
- {
- case FW_kOK:
- {
- FW_CAcquireCFMResourceAccess a(ev);
- ::GetIndString(but1, kDialogID, 1); // OK
- alertRefCon.fOkItemId = 1;
- alertRefCon.fCancelItemId = 0;
- items[0] = FW_kOKButtonPressed;
- break;
- }
- case FW_kOKCancel:
- {
- FW_CAcquireCFMResourceAccess a(ev);
- ::GetIndString(but1, kDialogID, 1); // OK
- ::GetIndString(but2, kDialogID, 2); // Cancel
- if (defaultButton == FW_kDefaultButton2)
- {
- alertRefCon.fCancelItemId = 0;
- alertRefCon.fOkItemId = 2;
- }
- else
- {
- alertRefCon.fOkItemId = 1;
- alertRefCon.fCancelItemId = 2;
- }
- items[0] = FW_kOKButtonPressed;
- items[1] = FW_kCancelButtonPressed;
- break;
- }
- case FW_kAbortRetryIgnore:
- {
- FW_CAcquireCFMResourceAccess a(ev);
- ::GetIndString(but1, kDialogID, 3); // Abort
- ::GetIndString(but2, kDialogID, 4); // Retry
- ::GetIndString(but3, kDialogID, 5); // Ignore
- alertRefCon.fOkItemId = defaultButton + 1;
- alertRefCon.fCancelItemId = 0;
- items[0] = FW_kAbortButtonPressed;
- items[1] = FW_kRetryButtonPressed;
- items[2] = FW_kIgnoreButtonPressed;
- break;
- }
- case FW_kYesNoCancel:
- {
- FW_CAcquireCFMResourceAccess a(ev);
- ::GetIndString(but1, kDialogID, 6); // Yes
- ::GetIndString(but2, kDialogID, 7); // No
- ::GetIndString(but3, kDialogID, 2); // Cancel
- if (defaultButton == FW_kDefaultButton1)
- {
- alertRefCon.fOkItemId = 1;
- alertRefCon.fCancelItemId = 3;
- }
- else if (defaultButton == FW_kDefaultButton2)
- {
- alertRefCon.fOkItemId = 2;
- alertRefCon.fCancelItemId = 3;
- }
- else
- {
- // I can't decide if Esc means Yes or No
- alertRefCon.fOkItemId = 3;
- alertRefCon.fCancelItemId = 0;
- }
- items[0] = FW_kYesButtonPressed;
- items[1] = FW_kNoButtonPressed;
- items[2] = FW_kCancelButtonPressed;
- moveButton = TRUE;
- break;
- }
- case FW_kYesNo:
- {
- FW_CAcquireCFMResourceAccess a(ev);
- ::GetIndString(but1, kDialogID, 6); // Yes
- ::GetIndString(but2, kDialogID, 7); // No
- alertRefCon.fCancelItemId = 0;
- if (defaultButton == FW_kDefaultButton2)
- alertRefCon.fOkItemId = 2;
- else
- alertRefCon.fOkItemId = 1;
- items[0] = FW_kYesButtonPressed;
- items[1] = FW_kNoButtonPressed;
- break;
- }
- case FW_kRetryCancel:
- {
- FW_CAcquireCFMResourceAccess a(ev);
- ::GetIndString(but1, kDialogID, 4); // Retry
- ::GetIndString(but2, kDialogID, 2); // Cancel
- if (defaultButton == FW_kDefaultButton2)
- {
- alertRefCon.fCancelItemId = 0;
- alertRefCon.fOkItemId = 2;
- }
- else
- {
- alertRefCon.fOkItemId = 1;
- alertRefCon.fCancelItemId = 2;
- }
- items[0] = FW_kRetryButtonPressed;
- items[1] = FW_kCancelButtonPressed;
- break;
- }
- default:
- FW_ASSERT(0); // unknown dialogFlags
- break;
- }
-
- MacSetButtonText(dlg, kButton1Id, but1, FALSE);
- MacSetButtonText(dlg, kButton2Id, but2, moveButton);
- MacSetButtonText(dlg, kButton3Id, but3, FALSE);
-
- // ----- Icons
- Handle hIcon = NULL;
- switch (iconType)
- {
- case FW_kStopAlert:
- hIcon = ::GetIcon(stopIcon);
- break;
-
- case FW_kCautionAlert:
- hIcon = ::GetIcon(cautionIcon);
- break;
-
- case FW_kNoteAlert:
- hIcon = ::GetIcon(noteIcon);
- break;
- };
-
- ::GetDialogItem(dlg, kIconId, &type, &handle, &rect);
- if (hIcon)
- {
- ::SetDialogItem(dlg, kIconId, type, hIcon, &rect);
- }
- else
- {
- ::GetDialogItem(dlg, kIconId, &type, &handle, &rect);
- short left = rect.left;
- ::OffsetRect(&rect, 1000, 0);
- ::SetDialogItem(dlg, kIconId, type, hIcon, &rect);
-
- ::GetDialogItem(dlg, kMessageId, &type, &handle, &rect);
- rect.left = left;
- ::SetDialogItem(dlg, kMessageId, type, handle, &rect);
- }
-
- if (beep)
- ::SysBeep(16);
-
- ::ShowWindow(dlg);
-
- ModalFilterUPP filterProc = NewModalFilterProc(&MacAlertDialogFilter);
- do
- {
- ::ModalDialog(filterProc, &item);
- } while (item <kButton1Id || item > kButton3Id);
-
- DisposeRoutineDescriptor(filterProc);
-
- }
- FW_CATCH_BEGIN
- FW_CATCH_REFERENCE(FW_XException, exception)
- {
- if (dlg)
- ::DisposeDialog(dlg);
- ::SetPort(curPort);
- FW_SetException(ev, exception);
- }
- FW_CATCH_EVERYTHING ()
- {
- if (dlg)
- ::DisposeDialog(dlg);
- ::SetPort(curPort);
- FW_SetEvError(ev, kODErrUndefined);
- }
- FW_CATCH_END
-
- ::DisposeDialog(dlg);
-
- ::SetPort(curPort);
-
- return (items[item - 1]);
- #endif
- }
-